﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using House.Service.Entities;
namespace House.Service
{
    public class BaseService<T> where T : BaseEntity
    {
        protected Model DB { get; set; }

        public BaseService(Model db)
        {
            DB = db;
        }
        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>添加后的对象Id</returns>
        public long Add(T model)
        {
            DB.Entry<T>(model).State = System.Data.Entity.EntityState.Added;
            DB.SaveChanges();
            return model.Id;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>添加成功返回true，否则返回false</returns>
        public bool AddBool(T model)
            {
            DB.Entry<T>(model).State = System.Data.Entity.EntityState.Added;

            return DB.SaveChanges()>0;

        }

        #endregion

        #region 删除
        /// <summary>
        /// 删除（软删除）
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>删除成功返回true，否则返回false</returns>
        public bool Delete(T model)
        {
            DB.Set<T>().Attach(model);
            model.IsDeleted = true;
            DB.Entry<T>(model).State = System.Data.Entity.EntityState.Modified;
            return DB.SaveChanges()>0;
        }

        public bool Delete(long id)
        {
            var t = DB.Set<T>().Find(id);
            if (t == null || t.IsDeleted)
            {
                return true;
            }
            t.IsDeleted = true;
            return DB.SaveChanges() > 0;
        }
        #endregion

        #region 修改

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>修改成功返回true，否则返回false</returns>
        public bool Update(T model)
        {
            DB.Set<T>().Attach(model);
            
            DB.Entry<T>(model).State = System.Data.Entity.EntityState.Modified;
            return DB.SaveChanges() > 0;
        }

        #endregion

        #region 查询单个实体

        public T Get(Expression<Func<T, bool>> whereLambda)
        {
            return DB.Set<T>().FirstOrDefault(whereLambda);
        }

        #endregion

        #region 查询列表

        public IQueryable<T> GetList(Expression<Func<T, bool>> whereLambda)
        {
            return DB.Set<T>().Where(p=>p.IsDeleted==false).Where(whereLambda);
        }
       /// <summary>
       /// 查询排序列表
       /// </summary>
       /// <typeparam name="TKey"></typeparam>
       /// <param name="whereLambda">查询条件</param>
       /// <param name="orderBy">排序条件</param>
       /// <param name="isAsc">排序后的列表</param>
       /// <returns></returns>
        public IQueryable<T> GetList<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderLambda, bool isAsc = true)
        {
            //return GetList(whereLambda).OrderBy(orderBy);
            var list = GetList(whereLambda);
            if (isAsc)
            {
                return list.OrderBy(orderLambda);
            }
            else 
            {
                return list.OrderByDescending(orderLambda);
            }

        }
        /// <summary>
        /// 查询分页列表
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="start">查询的开始位置</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="rowCount">查询结果总行数</param>
        /// <param name="whereLambda">查询条件</param>
        /// <param name="orderBy">排序条件</param>
        /// <param name="isAsc">是否正序</param>
        /// <returns></returns>
        public IQueryable<T> GetPagedList<TKey>(int start, int pageSize, ref long rowCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderBy, bool isAsc = true)
        {
            //获取排序后的查询结果
            var list = GetList(whereLambda,orderBy,isAsc);
            rowCount = list.LongCount();

            return list.Skip(start).Take(pageSize);
        }

        #endregion


    }
}
